//-----------------------------------------------------------------------
//<copyright file="BienBanNhapHangBLL.cs
//Code gen by tool.
//</copyright>
//-----------------------------------------------------------------------
using System;
using PMS.Exceptions;
using System.Collections.Generic;
using PMS.DAL;
using PMS.Entity;
using System.Transactions;

namespace PMS.BLL
{
	public class BienBanNhapHangBO : BaseBO
	{
		// Interface Instant Object
		private readonly IBienBanNhapHangDAO bienbannhaphangInterfaceObject = null;
        private readonly ChiTietBienBanNhapHangBO ChiTietBBNHInterfaceObject = null;

		// Constructor
		public BienBanNhapHangBO()
		{
			bienbannhaphangInterfaceObject = new DAL.ServiceFactory<IBienBanNhapHangDAO>().CurrentDAL;
            ChiTietBBNHInterfaceObject = new ChiTietBienBanNhapHangBO();
		}

		#region BienBanNhapHang

		/// <summary>
		///
		/// </summary>
		/// <param name="bienbannhaphang"></param>
        public BienBanNhapHang CreateBienBanNhapHang(BienBanNhapHang bienbannhaphang, ChiTietBienBanNhapHangs DetailList)
		{
            string newstring = bienbannhaphang.MaBBNH;
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    bienbannhaphang = bienbannhaphangInterfaceObject.CreateBienBanNhapHang(bienbannhaphang);
                    foreach (ChiTietBienBanNhapHang item in DetailList)
                    {
                        item.MaBBNH = bienbannhaphang.MaBBNH;
                        ChiTietBBNHInterfaceObject.CreateChiTietBienBanNhapHang(item);
                    }
                }
                catch (Exception ex)
                {
                    bienbannhaphang.MaBBNH = newstring;
                    throw BusinessException.GetException("Business Access CreateBienBanNhapHang Exception", ex);
                }
                scope.Complete();
            }
            return bienbannhaphang;
		}

		/// <summary>
		///
		/// </summary>
		/// <param name="bienbannhaphang"></param>
		public void UpdateBienBanNhapHang(BienBanNhapHang bienbannhaphang, ChiTietBienBanNhapHangs DetailList)
		{
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    bienbannhaphangInterfaceObject.UpdateBienBanNhapHang(bienbannhaphang);
                    ChiTietBBNHInterfaceObject.DeleteChiTietBienBanNhapHangByMaBBNH(bienbannhaphang.MaBBNH);
                    foreach (ChiTietBienBanNhapHang item in DetailList)
                    {
                        item.MaBBNH = bienbannhaphang.MaBBNH;
                        ChiTietBBNHInterfaceObject.CreateChiTietBienBanNhapHang(item);
                    }
                }
                catch (Exception ex)
                {
                    throw BusinessException.GetException("Business Access UpdatePhieuNhapException", ex);
                }
                scope.Complete();
            }
		}

		/// <summary>
		///
		/// </summary>
		/// <param name="bienbannhaphang"></param>
		public void DeleteBienBanNhapHang(BienBanNhapHang bienbannhaphang)
		{
			try
			{
                ChiTietBBNHInterfaceObject.DeleteChiTietBienBanNhapHangByMaBBNH(bienbannhaphang.MaBBNH);
				bienbannhaphangInterfaceObject.DeleteBienBanNhapHang(bienbannhaphang);
			}
			catch (Exception ex)
			{
				throw BusinessException.GetException("Business Access DeleteBienBanNhapHangException", ex);
			}
		}

		/// <summary>
		///
		/// </summary>
		public BienBanNhapHangs GetAllBienBanNhapHang()
		{
			try
			{
				return bienbannhaphangInterfaceObject.GetAllBienBanNhapHang();
			}
			catch (Exception ex)
			{
				throw BusinessException.GetException("Business Access GetAllBienBanNhapHang Exception", ex);
			}
		}

		/// <summary>
		///
		/// </summary>
		/// <param name="maBBNH"></param>
		public BienBanNhapHang GetBienBanNhapHangByID(string maBBNH)
		{
			try
			{
				return bienbannhaphangInterfaceObject.GetBienBanNhapHangByID(maBBNH);
			}
			catch (Exception ex)
			{
				throw BusinessException.GetException("Business Access GetBienBanNhapHangBy ID Exception", ex);
			}
		}

		#endregion
	}
}
